package com.antis.olsl.main.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.antis.olsl.library.utils.DensityUtil;
import com.antis.olsl.main.R;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.popuwindow
 * @ClassName: FunctionPopupWindow
 * @Description: java类作用描述
 * @Author: 羊琪
 * @CreateDate: 2020/4/8 18:49
 * @UpdateDate: 2020/4/8 18:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FunctionPopupWindow {
    PopupWindow operationPop;
    Context context;
    View markView;
    FunctionMenuClickListener listener;

    public FunctionPopupWindow(Context context, View markView, FunctionMenuClickListener listener) {
        this.context = context;
        this.markView = markView;
        this.listener = listener;
        init();
    }

    public interface FunctionMenuClickListener {
        void onClickFunctionMenuListener(int index);

        void onPopupWindowDismissListener();
    }

    private void init() {
        if (operationPop != null) {
            operationPop.dismiss();
        }
        View layout = LayoutInflater.from(context).inflate(R.layout.popu_choose_function, null);

        LinearLayout llWork = layout.findViewById(R.id.ll_work);
        LinearLayout llData = layout.findViewById(R.id.ll_data);
        LinearLayout llMagicCube = layout.findViewById(R.id.ll_magicCube);
        LinearLayout llSetting = layout.findViewById(R.id.ll_setting);

        llWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClickFunctionMenuListener(0);
            }
        });

        llData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClickFunctionMenuListener(1);
            }
        });
        llMagicCube.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClickFunctionMenuListener(2);
            }
        });
        llSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClickFunctionMenuListener(3);
            }
        });

        operationPop = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // 可聚焦
        operationPop.setFocusable(true);
        // 外部可点击
        operationPop.setOutsideTouchable(true);
        operationPop.update();
        // 点击返回键隐藏（需要给PopuWindow设置背景）
//        operationPop.setBackgroundDrawable(new ColorDrawable(0xffffffff));
        operationPop.showAsDropDown(markView, (DensityUtil.dip2px(context, 20)), 0);  //显示位置设置在view这个控件下面
//        operationPop.showAtLocation(view, Gravity.RIGHT,-(DensityUtil.dip2px(this,20)),0);
        ColorDrawable cd = new ColorDrawable();//必须设置此属性才能点击外部关闭
        operationPop.setBackgroundDrawable(cd);

        operationPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//               setActivityTranslate(false);
                listener.onPopupWindowDismissListener();
            }
        });
//        setActivityTranslate(true);
    }

    public void dismiss() {
        if (operationPop != null) {
            operationPop.dismiss();
        }
    }

    private void setActivityTranslate(boolean translate) {
        WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
        params.alpha = translate ? 0.5f : 1.0f;
        ((Activity) context).getWindow().setAttributes(params);
    }
}
